package com.agy.filemanager.service.impl;

import com.agy.filemanager.common.PageUtil;
import com.agy.filemanager.common.StringUtils;
import com.agy.filemanager.entity.ProType;
import com.agy.filemanager.entity.ProTypeExample;
import com.agy.filemanager.mapper.ProTypeMapper;
import com.agy.filemanager.service.ProTypeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProTypeServiceImpl implements ProTypeService {
    @Autowired
    private ProTypeMapper proTypeMapper;

    @Override
    public PageUtil<ProType> getProTypeByMainTypeId(int page, int rows, String mainTypeId) {
        //创建容器
        PageUtil<ProType> pageContent = new PageUtil<>();
        //设置分页
        PageHelper.startPage(page,rows);
        ProTypeExample example = new ProTypeExample();
        example.setOrderByClause("SORT ASC");
        ProTypeExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(mainTypeId);
        List<ProType> proTypes = proTypeMapper.selectByExample(example);
        //查询总条数
        long total = proTypeMapper.selectCount();
        pageContent.setTotal(total);
        pageContent.setRows(proTypes);
        return pageContent;
    }

    @Override
    public String insertProTypeBasicData(ProType proType) {
        //获取主键id
        String id = StringUtils.getUUID();
        proType.setId(id);
        proTypeMapper.insertSelective(proType);
        return id;
    }

    @Override
    public void updateProTypeBasicData(ProType proType) {
        proTypeMapper.updateByPrimaryKeySelective(proType);
    }

    @Override
    public void deleteProTypeBasicData(ProType proType) {
        proTypeMapper.updateByPrimaryKeySelective(proType);
    }

    @Override
    public PageUtil<ProType> getProTypeList(String mainTypeId,String name) {
        PageUtil<ProType> page = new PageUtil<>();
        ProTypeExample example = new ProTypeExample();
        example.setOrderByClause("SORT ASC");
        ProTypeExample.Criteria criteria = example.createCriteria();
        criteria.andIsyesEqualTo(true);
        criteria.andCodeEqualTo(mainTypeId);
        if(name != null && !name.equals("")){
            criteria.andNameLike("%"+name+"%");
        }
        List<ProType> proTypes = proTypeMapper.selectByExample(example);
        page.setRows(proTypes);
        page.setTotal(proTypes.size());
        return page;
    }
}
